package com.shire.shire_interface.controller.keyword;

import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.keyword.WxKeywordReply;
import com.shire.shire_interface.model.keyword.WxReplyRecord;
import com.shire.shire_interface.model.keyword.WxReplyRecordExample;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.keyword.WxKeywordReplyService;
import com.shire.shire_interface.service.keyword.WxReplyRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "群管理 关键词回复")
@RestController
@RequestMapping("/keyword")
public class WxKeywordReplyController extends BaseController {

    @Resource
    private WxKeywordReplyService wxKeywordReplyService;
    @Resource
    private WxReplyRecordService wxReplyRecordService;
    @Resource
    private WxGroupService wxGroupService;

//    @ApiOperation(value = "插入设置 csid，wxaccount，keyword，replytype 回复类型 0:文本 1:连接 2:图片 3:文件 4:名片 5:公众号 6:材料库，msgcontent, groupNoList")
    @ResponseBody
    @PostMapping("/insertSetting")
    public Response insert(@RequestBody WxKeywordReply wxKeywordReply, HttpSession session){
        Response response = new Response();
        try {
            Integer csid = wxKeywordReply.getCsid();
            String wxaccount = wxKeywordReply.getWxaccount();
            if (!checkLoginUserByCsId(session, csid)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            List<String> groupNoList = wxKeywordReply.getGroupNoList();
            if (wxKeywordReply.getType()!=null && wxKeywordReply.getType() == 0) {
                wxKeywordReply.setItemcount(groupNoList.size());
            }
            wxKeywordReply.setCreateTime(new Date());

            wxKeywordReplyService.insert(wxKeywordReply);

            if (wxKeywordReply.getType()!=null && wxKeywordReply.getType() == 0) {
                for (String groupNo : groupNoList) {
                    WxReplyRecord wxReplyRecord = new WxReplyRecord();
                    wxReplyRecord.setCsid(csid);
                    wxReplyRecord.setWxaccountid(wxaccount);
                    wxReplyRecord.setGroupaccount(groupNo);
                    wxReplyRecord.setReplyruleid(wxKeywordReply.getId());
                    wxReplyRecordService.insert(wxReplyRecord);
                }
            }

            Integer id = wxKeywordReply.getId();

            WxReplyRecordExample wxReplyRecordExample = new WxReplyRecordExample();
            wxReplyRecordExample.createCriteria().andCsidEqualTo(csid).andWxaccountidEqualTo(wxaccount).andReplyruleidEqualTo(id);
            List<WxReplyRecord> wxReplyRecords = wxReplyRecordService.selectByExample(wxReplyRecordExample);

            //TODO 优化查询
            for (WxReplyRecord wxReplyRecord : wxReplyRecords) {
                WxGroupExample wxGroupExample = new WxGroupExample();
                wxGroupExample.createCriteria().andCsidEqualTo(csid).andWxaccountEqualTo(wxaccount).andGroupnoEqualTo(wxReplyRecord.getGroupaccount());
                List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                wxReplyRecord.setGroups(wxGroups);
            }

            wxKeywordReply.setWxReplyRecord(wxReplyRecords);

            response.setData(wxKeywordReply);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

    @ResponseBody
    @PostMapping("/updateSetting")
    public Response update(@RequestBody WxKeywordReply wxKeywordReply, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxKeywordReply.setUpdateTime(new Date());
            wxKeywordReplyService.update(wxKeywordReply);
            response.setData(wxKeywordReply);
            response.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }


    /*@ApiOperation(value = "查询设置csId, wxAccount")
    @ResponseBody
    @PostMapping("/getSetting")
    public Response getSetting(@RequestBody JSONObject jsonObject){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");

            WxKeywordReplyExample wxKeywordReplyExample = new WxKeywordReplyExample();
            wxKeywordReplyExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andStatusEqualTo(0);
            List<WxKeywordReply> wxKeywordReplies = wxKeywordReplyService.selectByExample(wxKeywordReplyExample);
            response.setData(wxKeywordReplies);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }*/

 /*   @ApiOperation(value = "删除设置 id")
    @ResponseBody
    @PostMapping("/delSetting")
    public Response delSetting(@RequestBody JSONObject jsonObject){
        Response response = new Response();
        try {
            Integer id = jsonObject.getInteger("id");
            wxKeywordReplyService.deleteByPrimaryKey(id);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }*/
}
